package com.huihui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.huihui.R;
import com.huihui.activity.CommentListActivity;
import com.huihui.bean.Comment;
import com.huihui.bean.Goods;
import com.huihui.datamanager.AppManager;
import com.huihui.network.HttpContants;
import com.huihui.network.OkHttpRequest;
import com.huihui.network.base.CallBackUtil;
import com.huihui.utils.IntentUtils;
import com.huihui.utils.LoadImageUtil;
import com.huihui.utils.ToolUtils;
import com.huihui.widget.PartColorTextView;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;


/**
 * Created by Administrator on 2018/8/18.
 * 详情适配器
 */

public class DetialAdapter extends RecyclerView.Adapter<ItemViewHolder> {

    private final long relation_id;
    private int class_id;

    private Context mContext;
    private List<Comment> list;
    private MineZanGridViewAdapter adapter;
    //创建一个头布局的View
    public View mHeader;
    //创建两个字段
    public final static int TYPE_HEAD = 0;
    public final static int TYPE_NORMAL = 1;
    private int isLike;

    public DetialAdapter(Context context, List<Comment> list, long relation_id, int class_id, int isLike) {
        this.mContext = context;
        this.list = list;
        this.relation_id = relation_id;
        this.class_id = class_id;
        this.isLike = isLike;
    }

    @Override
    public int getItemCount() {
        if (mHeader == null) {
            if (list == null) {
                return 0;
            } else {
                return list.size();
            }
        } else {
            if (list == null) {
                return 1;
            } else {
                return list.size() + 1;
            }
        }

//        return mHeader == null ? list.size() : list.size() + 1;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewHolder holder = null;
        if (viewType == TYPE_HEAD) {
            holder = new ItemViewHolder(mHeader);
        } else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.activity_detail_item_layout, parent, false);
            holder = new ItemViewHolder(view);
        }
        return holder;
    }


    @Override
    public void onBindViewHolder(final ItemViewHolder holder, int position) {
        //判断是否存在头部
        if (!isHeadState(position)) {
            //如果存在头部，则对position 减1
            if (mHeader != null) {
                position--;
            }
            if(list.size() == 0){
                return;
            }
            final Comment comment = list.get(position);
            String content = comment.getCom_content();
            LoadImageUtil.displayImage(mContext, comment.getUser_head_photo(), holder.head_view);
            holder.user_name.setText(comment.getUser_nick_name());
            holder.user_time.setText(comment.getCom_time());
            holder.content.setText(content);
            holder.zanCount.setText(String.valueOf(comment.getCollection_num()));
//            holder.zan.setText(comment.getSub_comm());
            List<Comment> childComment = comment.getSub_comm();
//            if (childComment.size() > 0) {
//                //暂时注释
//                holder.childLayout.setVisibility(View.VISIBLE);
////                holder.replyName.setText(childComment.get(0).getUser_nick_name()+":"+childComment.get(0).getCom_content());
//                String allText = childComment.get(0).getUser_nick_name() + ":" + childComment.get(0).getCom_content();
//                holder.replyName.setPartText(allText, childComment.get(0).getUser_nick_name(), mContext.getResources().getColor(R.color.color_333333), mContext.getResources().getColor(R.color.color_feba54));
//            } else {
//                //暂时注释
//                holder.childLayout.setVisibility(View.GONE);
//            }
            holder.childLayout.setVisibility(View.VISIBLE);
            final List<Comment> tempChileCommentList = new ArrayList<>();
            if (childComment.size() == 0) {
                holder.childLayout.setVisibility(View.GONE);
            } else if (childComment.size() <= 3) {
                tempChileCommentList.addAll(childComment);
                holder.replyCount.setVisibility(View.GONE);
            } else if (childComment.size() > 3) {
                tempChileCommentList.add(childComment.get(0));
                tempChileCommentList.add(childComment.get(1));
                tempChileCommentList.add(childComment.get(2));
                holder.replyCount.setVisibility(View.VISIBLE);
                holder.replyCount.setText("共" + childComment.size() + "条回复>");
                holder.replyCount.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(comment == null){
                            return;
                        }
                        IntentUtils.gotoCommentListActivity(mContext, comment,relation_id,class_id);
                    }
                });
            }
            ChildCommentAdapter childCommentAdapter = new ChildCommentAdapter(mContext, R.layout.chile_comment_item, tempChileCommentList);
            holder.child_comment_list.setAdapter(childCommentAdapter);
            int listViewHeight = ToolUtils.getListViewHeight(holder.child_comment_list);
            ViewGroup.LayoutParams layoutParams = holder.child_comment_list.getLayoutParams();
            layoutParams.height = listViewHeight;
            holder.child_comment_list.setLayoutParams(layoutParams);

            holder.child_comment_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if(comment == null){
                        return;
                    }
                    IntentUtils.gotoCommentListActivity(mContext, comment,relation_id,class_id);

                }
            });
            holder.search_detail_zan_icon.setTag(position);
            holder.search_detail_zan_icon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    int p = (int) v.getTag();
                    if (comment.getIs_like() == 0) {
                        addZan(p);
                    } else {
                        removeZan(p);
                    }

                }
            });
            if (comment.getIs_like() == 0) {
                holder.search_detail_zan_icon.setImageResource(R.mipmap.zan);
            } else {
                holder.search_detail_zan_icon.setImageResource(R.mipmap.zan_press);
            }
        }


        if (onItemClickListener != null) {
            if (holder.content == null) {
                return;
            }
            holder.item_layout.setTag(position);
            holder.item_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onClick((int) v.getTag());
                }
            });
            holder.item_layout.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    onItemClickListener.onLongClick((int) v.getTag());
                    return false;
                }
            });
        }

    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onClick(int position);

        void onLongClick(int position);
    }

    public void addHeader(View view) {
        this.mHeader = view;
    }

    @Override
    public int getItemViewType(int position) {
        if (isHeadState(position)) {
            return TYPE_HEAD;
        } else {
            return TYPE_NORMAL;
        }
    }

    //用一个方法来判断是不是创建了头布局
    private boolean isHeadState(int position) {
        return mHeader != null && position == 0;
    }

    private void addZan(int position) {
        final Comment comment = list.get(position);
        //点赞的评论ID（如果是内容点赞此处为0）
        int relation_comment_id = comment.getCom_id();
        OkHttpRequest.getInstance().addCollection(HttpContants.addCollection,
                AppManager.getUserInfo().getUser_id(),
                relation_id,
                class_id,
                relation_comment_id,
                new CallBackUtil.CallBackString() {
                    @Override
                    public void onFailure(Call call, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject productObject = new JSONObject(response);
                            if (productObject != null && productObject.has(HttpContants.STATE) && productObject.getInt(HttpContants.STATE) == 10000) {
                                int collection_num = comment.getCollection_num();
                                comment.setCollection_num(++collection_num);
                                comment.setIs_like(1);
                                notifyDataSetChanged();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void removeZan(int p) {
        final Comment comment = list.get(p);
        int comment_id = comment.getCom_id();
        OkHttpRequest.getInstance().removeCollection(HttpContants.removeCollection, AppManager.getUserInfo().getUser_id(),
                relation_id, class_id, comment_id, new CallBackUtil.CallBackString() {
                    @Override
                    public void onFailure(Call call, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject productObject = new JSONObject(response);
                            if (productObject != null && productObject.has(HttpContants.STATE) && productObject.getInt(HttpContants.STATE) == 10000) {
                                comment.setCollection_num(comment.getCollection_num() - 1);
                                comment.setIs_like(0);
                                notifyDataSetChanged();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private class ChildCommentAdapter extends CommonAdapter<Comment> {

        public ChildCommentAdapter(Context context, int layoutId, List<Comment> datas) {
            super(context, layoutId, datas);
        }

        @Override
        protected void convert(ViewHolder viewHolder, Comment item, int position) {
            PartColorTextView textView = viewHolder.getView(R.id.child_comment_textview);
            String allText = item.getUser_nick_name() + ":" + item.getCom_content();
            textView.setPartText(allText, item.getUser_nick_name(), mContext.getResources().getColor(R.color.color_333333), mContext.getResources().getColor(R.color.color_f38212));
        }
    }
}
